home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj1291.zip / FS.ASC < prev    next >
Text File  |  1991-11-11  |  8KB  |  253 lines

  1. _FS: A FILE STATUS UTILITY FOR UNIX_
  2. by Jeff Reagen
  3.  
  4. [LISTING ONE]
  5.  
  6. /* Copyright (c) 1991 Jeff Reagen ALL RIGHTS RESERVED. */
  7. #include "sys/types.h"
  8. #include "sys/fcntl.h"
  9. #include "sys/param.h"
  10. #include "sys/immu.h"
  11. #include "sys/fs/s5dir.h"
  12. #include "sys/signal.h"
  13. #include "sys/user.h"
  14. #include "sys/errno.h"
  15. #include "sys/cmn_err.h"
  16. #include "sys/buf.h"
  17. #include "nlist.h"
  18. #include "sys/stat.h"
  19. #include "sys/region.h"
  20. #include "sys/proc.h"
  21. #include "sys/var.h"
  22. #include "sys/sysmacros.h"
  23. #include "sys/file.h"
  24. #include "sys/inode.h"
  25. #include "sys/utsname.h"
  26.  
  27. long lseek();
  28.  
  29. /* Symbols fs will request from the kernel. */
  30. struct nlist kern_syms[] = {
  31.         "v",
  32.         0,
  33.         0,
  34.         0,
  35.         0,
  36.         0,
  37.  
  38.         "file",
  39.         0,
  40.         0,
  41.         0,
  42.         0,
  43.         0,
  44.  
  45.         "",
  46.         0,
  47.         0,
  48.         0,
  49.         0,
  50.         0
  51. };
  52.  
  53. struct  nlist *pinfo;
  54. struct  var   v;
  55. int     fd;
  56. int     inofd;  /* file pointer for inode lookup */
  57.  
  58.  
  59.  
  60.  
  61. [LISTING TWO]
  62.  
  63. /* Copyright (c) 1991 Jeff Reagen ALL RIGHTS RESERVED. */
  64. main ()
  65. {
  66.         int     i;
  67.         long    res;
  68.         struct  utsname name;
  69.         char    realname[10];
  70.  
  71.         /*  Obtain name of current kernel. It's not necessarily /unix! */
  72.         if (uname (&name) < 0)
  73.         {
  74.            printf ("Cannot identify the current Unix system!\n");
  75.            exit (1);
  76.         }
  77.  
  78.         /* Prefix name of kernel with a "/" */
  79.         sprintf (realname, "/%s", name.sysname);
  80.  
  81.         if (nlist (realname, kern_syms) < 0)
  82.         {
  83.                 printf ("Could not get name list\n");
  84.                 exit (1);
  85.         }
  86.         pinfo = &kern_syms[0];
  87.  
  88.         /* Check value of proc symbol. */
  89.         if (pinfo->n_value == 0)
  90.         {
  91.                 printf ("nlist call failed.\n");
  92.                 exit (2);
  93.         }
  94.         if ( (fd = open ("/dev/kmem", O_RDONLY)) < 0)
  95.         {
  96.                 printf ("Cannot open /dev/kmem.\n");
  97.                 exit (3);
  98.         }
  99.         if ( (inofd = open ("/dev/kmem", O_RDONLY)) < 0)
  100.         {
  101.                 printf ("Cannot open /dev/kmem.\n");
  102.                 exit (3);
  103.         }
  104.  
  105.         /* Get Unix system variable structure. */
  106.         if ( (res=lseek (fd, (long)kern_syms[0].n_value, 0)) == -1)
  107.         {
  108.                 printf ("Can't seek /dev/kmem.\n");
  109.                 exit (4);
  110.         }
  111.         if (read (fd, &v, sizeof (struct var)) < 0)
  112.         {
  113.                 printf ("Can't read sysinfo struct from /dev/kmem.\n");
  114.                 exit (5);
  115.         }
  116.  
  117.         /* Display system information. */
  118.         file_entry();
  119. }
  120.  
  121.  
  122.  
  123.  
  124. [LISTING THREE]
  125.  
  126. /* Copyright (c) 1991 Jeff Reagen ALL RIGHTS RESERVED. */
  127. /* Examine each entry in the Unix file table. */
  128. file_entry()
  129. {
  130.    int res;
  131.    int fno;
  132.    struct file file;
  133.  
  134.         /* Position to base of file table. */
  135.         if ( (res=lseek (fd, (long)kern_syms[1].n_value, 0)) == -1)
  136.         {
  137.                 printf ("Can't seek /dev/kmem.\n");
  138.                 exit (4);
  139.         }
  140.  
  141.         /* read each entry one after the other. */
  142.         for (fno = 0; fno < v.v_file; fno++)
  143.         {
  144.            /* get next slot. */
  145.            if (read (fd, &file, sizeof (struct file)) < 0)
  146.            {
  147.                 printf ("Can't read file slot.\n");
  148.                 exit (5);
  149.            }
  150.            /* Display entry info. iff referenece count indicates 
  151.               file entry is valid. */
  152.            if (!file.f_count)
  153.            {
  154.               continue;   /* abort this entry! */
  155.            }
  156.            printf ("Contents of file table entry #%d\n",fno);
  157.            printf ("\tFile operation flag ................. ");
  158.            switch (file.f_flag)
  159.            {
  160.                 case FOPEN:   printf ("File is opened\n");
  161.                               break;
  162.                 case FREAD:   printf ("File is being read\n");
  163.                               break;
  164.                 case FWRITE:  printf ("File is being written\n");
  165.                               break;
  166.                 case FREAD|FWRITE:
  167.                               printf ("File opened for read/write\n");
  168.                               break;
  169.                 case FWRITE|FAPPEND:
  170.                               printf ("File opened for write append\n");
  171.                               break;
  172.                 case FREAD|FWRITE|FAPPEND:
  173.                               printf ("File opened for read/write appends\n");
  174.                               break;
  175.                 case FNDELAY: printf ("File operating in delayed mode\n");
  176.                               break;
  177.                 case FAPPEND: printf ("File opened for appended operation\n");
  178.                               break;
  179.                 case FSYNC:   printf ("File writes are synchronous\n");
  180.                               break;
  181.                 case FMASK:   printf ("File masked ?????\n");
  182.                               break;
  183.                 default:      printf ("%x\n",file.f_flag);
  184.                               break;
  185.            }
  186.            printf ("\tReference count ..................... %d\n",
  187.                         file.f_count);
  188.            printf ("\tFile pointer position  .............. %lx\n",
  189.                         file.f_un.f_off);
  190.  
  191.           /* process the inode */
  192.           inode_entry (file);
  193.  
  194.           /* Pause so user can read the output. */
  195.           getchar(); printf("\n\n");
  196.            
  197.         }
  198. }
  199.  
  200.  
  201.  
  202. [LISTING FOUR]
  203.  
  204. /* Copyright (c) 1991 Jeff Reagen ALL RIGHTS RESERVED. */
  205. /* Get the inode associated with the current file table entry. */
  206. inode_entry (file)
  207.   struct file file;
  208. {
  209.    int res;
  210.    struct inode inode;   /* current core inode */
  211.            if ( (res=lseek (inofd, (long)file.f_up.f_uinode, 0)) == -1)
  212.            {
  213.                 printf ("Can't seek /dev/kmem for inode.\n");
  214.                 exit (4);
  215.            }
  216.            if (read (inofd, &inode, sizeof (struct inode)) < 0)
  217.            {
  218.                 printf ("Can't read in core inode.\n");
  219.                 exit (5);
  220.            }
  221.        printf ("\tInode number ........................ %d\n", inode.i_number);
  222.        printf ("\tFile type ........................... ");
  223.        switch (inode.i_ftype)
  224.            {
  225.               case IFDIR: printf ("Directory\n");
  226.                           break;
  227.               case IFCHR: printf ("Character device (Maj %d/Min %d)\n",
  228.                                 major(inode.i_rdev),minor(inode.i_rdev));
  229.                           break;
  230.               case IFBLK: printf ("Block device\n");
  231.                           printf ("\tBlock device .... %d\n",inode.i_dev);
  232.                           break;
  233.               case IFREG: printf ("Regular file\n");
  234.         printf ("\tFile exists on Major/Minor device ... %d/%d\n",
  235.                 major(inode.i_dev), minor(inode.i_dev) );
  236.                           break;
  237.               case IFIFO: printf ("FIFO special\n");
  238.                           break;
  239.               case IFMPC: printf ("Multiplexed Character special\n");
  240.                           break;
  241.               case IFMPB: printf ("Multiplexed Block special\n");
  242.                           break;
  243.               case IFNAM: printf ("Special Named file\n");
  244.            }
  245.           printf ("\tFile size ........................... %d\n",inode.i_size);
  246.           printf ("\tUser id ............................. %d\n", inode.i_uid);
  247.           printf ("\tGroup id ............................ %d\n", inode.i_gid);
  248. }
  249.  
  250.  
  251.  
  252.  
  253.